package com.globallogic.socialnetwork.entity;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name = "users")
public class User implements HasId {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long idUser;

	private String city;

	private String login;

	private String name;

	private String password;

	private String sex;

	private String userInfo;

	// bi-directional many-to-many association to Group
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "usergroup", joinColumns = { @JoinColumn(name = "idUser") }, inverseJoinColumns = { @JoinColumn(name = "idGroup") })
//	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Group> groups;

	public User() {
	}

	public User(String login, String password, String name, String sex,
			String city, String userInfo) {
		super();
		this.city = city;
		this.login = login;
		this.name = name;
		this.password = password;
		this.sex = sex;
		this.userInfo = userInfo;
	}

	// bi-directional many-to-one association to Friend
	@OneToMany(mappedBy = "accepter")
	private List<Friend> accepter;

	// bi-directional many-to-one association to Friend
	@OneToMany(mappedBy = "proposer")
	private List<Friend> proposer;

	// bi-directional many-to-one association to Message
	@OneToMany(mappedBy = "sender")
	private List<Message> sender;

	// bi-directional many-to-one association to Message
	@OneToMany(mappedBy = "receiver")
	private List<Message> receiver;

	// bi-directional many-to-one association to Message
	@OneToMany(mappedBy = "keeper")
	private List<Message> keeper;

	@Override
	public Long getId() {
		return this.idUser;
	}

	@Override
	public void setId(Long idUser) {
		this.idUser = idUser;
	}

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getLogin() {
		return this.login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSex() {
		return this.sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(String userInfo) {
		this.userInfo = userInfo;
	}

	public List<Group> getGroups() {
		return this.groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public List<Friend> getAccepter() {
		return this.accepter;
	}

	public void setAccepter(List<Friend> accepter) {
		this.accepter = accepter;
	}

	public List<Friend> getProposer() {
		return this.proposer;
	}

	public void setProposer(List<Friend> proposer) {
		this.proposer = proposer;
	}

	public List<Message> getSender() {
		return this.sender;
	}

	public void setSender(List<Message> sender) {
		this.sender = sender;
	}

	public List<Message> getReceiver() {
		return this.receiver;
	}

	public void setReceiver(List<Message> receiver) {
		this.receiver = receiver;
	}

	public List<Message> getKeeper() {
		return this.keeper;
	}

	public void setKeeper(List<Message> keeper) {
		this.keeper = keeper;
	}


	@Override
	public String toString() {
		return "( " + idUser + " " + city + " " + login + " " + name + " "
				+ password + " " + sex + " " + userInfo + " )";
	}


	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(city).append(idUser).append(login)
				.append(name).append(password).append(sex).append(userInfo)
				.toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		return new EqualsBuilder().append(city,other.city).append(idUser,other.idUser).append(login,other.login)
		.append(name,other.name).append(password,other.password).append(sex,other.sex).append(userInfo,other.userInfo)
		.isEquals();
	}

}